package com.ruoyi.web.controller.test;

import com.ruoyi.common.utils.poi.ExcelImageExtractor;

import java.util.List;

/**
 * Excel二维码提取示例
 * 
 * 演示如何从Excel中提取二维码图片并转换为Base64
 * 
 * @author ruoyi
 */
public class ExcelQRCodeExtractorExample {
    
    /**
     * 示例1: 提取指定sheet的所有二维码
     */
    public static void example1() {
        String excelPath = "D:/test_output.xlsm";
        int qrCodeSheetIndex = 1; // 第二个sheet(索引从0开始)
        
        System.out.println("========== 示例1: 提取所有二维码 ==========");
        
        List<ExcelImageExtractor.ImageInfo> images = 
                ExcelImageExtractor.extractImagesFromSheet(excelPath, qrCodeSheetIndex);
        
        System.out.println("找到 " + images.size() + " 个二维码图片\n");
        
        for (int i = 0; i < images.size(); i++) {
            ExcelImageExtractor.ImageInfo image = images.get(i);
            System.out.println("二维码 #" + (i + 1) + ":");
            System.out.println("  Base64: " + image.getBase64());
            System.out.println();
        }
    }
    
    /**
     * 示例2: 提取指定位置的二维码
     */
    public static void example2() {
        String excelPath = "D:/test_output.xlsm";
        int sheetIndex = 1;
        int row = 0;     // 第1行
        int column = 0;  // 第1列
        
        System.out.println("========== 示例2: 提取指定位置的二维码 ==========");
        
        ExcelImageExtractor.ImageInfo image = 
                ExcelImageExtractor.extractImageAtPosition(excelPath, sheetIndex, row, column);
        
        if (image != null) {
            System.out.println("找到二维码:");
            System.out.println("  位置: [" + row + "," + column + "]");
            System.out.println("  格式: " + image.getFormat());
            System.out.println("  Base64: " + image.getBase64());
        } else {
            System.out.println("未找到二维码");
        }
    }
    
    /**
     * 示例3: 获取所有Base64列表
     */
    public static void example3() {
        String excelPath = "D:/test_output.xlsm";
        int sheetIndex = 1;
        
        System.out.println("========== 示例3: 获取Base64列表 ==========");
        
        List<String> base64List = 
                ExcelImageExtractor.extractImagesAsBase64List(excelPath, sheetIndex);
        
        System.out.println("提取到 " + base64List.size() + " 个Base64编码\n");
        
        for (int i = 0; i < base64List.size(); i++) {
            String base64 = base64List.get(i);
            System.out.println("Base64 #" + (i + 1) + ":");
            System.out.println(base64.substring(0, Math.min(100, base64.length())) + "...");
            System.out.println();
        }
    }
    
    /**
     * 示例4: 打印详细位置信息
     */
    public static void example4() {
        String excelPath = "D:/test_output.xlsm";
        int sheetIndex = 1;
        
        System.out.println("========== 示例4: 打印详细信息 ==========");
        
        ExcelImageExtractor.printImagePositions(excelPath, sheetIndex);
    }
    
    /**
     * 示例5: 验证二维码内容(需要集成zxing库)
     */
    public static void example5() {
        String excelPath = "D:/test_output.xlsm";
        int sheetIndex = 1;
        
        System.out.println("========== 示例5: 验证二维码内容 ==========");
        
        List<ExcelImageExtractor.ImageInfo> images = 
                ExcelImageExtractor.extractImagesFromSheet(excelPath, sheetIndex);
        
        for (int i = 0; i < images.size(); i++) {
            ExcelImageExtractor.ImageInfo image = images.get(i);
            
            System.out.println("二维码 #" + (i + 1) + ":");
            System.out.println("  Base64长度: " + image.getBase64().length());
            
            // 如果集成了zxing库,可以在这里解码二维码内容
            // String content = decodeQRCode(image.getData());
            // System.out.println("  二维码内容: " + content);
            
            // 也可以保存为文件进行验证
            // saveImageToFile(image.getData(), "qrcode_" + (i+1) + ".png");
            
            System.out.println();
        }
    }
    
    /**
     * 主方法 - 运行所有示例
     */
    public static void main(String[] args) {
        try {
            // 运行示例1
            example1();
            
            // 运行示例2
            // example2();
            
            // 运行示例3
            // example3();
            
            // 运行示例4
            // example4();
            
            // 运行示例5
            // example5();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
